package com.zxjtest.start;

import com.zxjtest.start.prop.TestPropertiesConfiguration;
import com.zxjtest.start.spring02.Aop2;
import com.zxjtest.start.spring02.Klass;
import com.zxjtest.start.spring02.School;
import com.zxjtest.start.spring02.Student;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.env.Environment;

import java.util.List;

/**
 * 8.（必做）给前面课程提供的 Student/Klass/School 实现自动配置和 Starter。
 */
// 由于此类添加到了spring.factories中，所以不需要此配置进行实例化
@Configuration
// 不加此配置的话，引入此包的项目无法创建com.zxjtest.start下的bean
@ComponentScan("com.zxjtest.start")
// 引入TestPropertiesConfiguration配置类，生成这个类的bean，若此类添加到spring.factories中，则不需要此配置
@EnableConfigurationProperties(TestPropertiesConfiguration.class)
// matchIfMissing = true不进行配置检测，matchIfMissing = false时检测application.properties中spring.zxj.enable是否为true，若没或为其他值，则不加载此配置
@ConditionalOnProperty(prefix = "spring.zxj", name = "enable", havingValue = "true", matchIfMissing = false)
//@AllArgsConstructor
public class TestAutoConfiguration implements EnvironmentAware {

    public final TestPropertiesConfiguration props;

    public TestAutoConfiguration(TestPropertiesConfiguration props) {
        this.props = props;
    }

    @Bean
    public Student student100() {
        Student student100 = new Student();
        student100.setId(100);
        student100.setName("KK100");
        return student100;
    }

    @Bean
    public Klass klass(List<Student> students) {
        Klass class1 = new Klass();
        class1.setStudents(students);
        return class1;
    }

    @Bean
    public School school() {
        School school = new School();
        return school;
    }

    @Bean
    public Aop2 aop2() {
        Aop2 aop2 = new Aop2();
        return aop2;
    }
    

    @Override
    public final void setEnvironment(final Environment environment) {
    }
}
